"""
Input: nums = [4,5,6,7,0,1,2], target = 0
Output: 4
Example 2:

Input: nums = [4,5,6,7,0,1,2], target = 3
Output: -1

"""
from typing import List


def bs(nums: List[int], target: int) -> int:
    if not nums:
        return -1
    left = 0
    right = len(nums) - 1
    while left < right:
        mid = (left + right) // 2
        # short cut
        if nums[mid] == target:
            return mid
        # left ordered
        if nums[left] < nums[mid]:
            if nums[left] <= target <= nums[mid]:
                right = mid
            else:
                left = mid + 1
        else:
            if nums[mid + 1] <= target <= nums[right]:
                left = mid + 1
            else:
                right = mid
    return left if nums[left] == target else -1

print(bs([4,5,6,7,0,1,2], 0))
print(bs([4,5,6,7,0,1,2], 3))

